home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tech Arsenal 1
/
Tech Arsenal (Arsenal Computer).ISO
/
tek-13
/
me_cd22.zip
/
MUTT2.ZIP
/
QUEEN.MUT
< prev
next >
Wrap
Lisp/Scheme
|
1992-04-27
|
2KB
|
84 lines
;; place N queens on a NxN board
;; C Durland Public Domain
(include me2.h)
(array small-int boardx 20 boardy 20)
(small-int nQueens N)
(defun
put-queen (int x y)(s)
{
(move-cursor (+ y 1) (+ 1 (* x 2)))(puts s)(update)
}
print-board
{
(move-cursor 0 0)
(puts ".---------------.^M^J")
(puts "|*|*|*|*|*|*|*|*|^M^J")
(puts "|*|*|*|*|*|*|*|*|^M^J")
(puts "|*|*|*|*|*|*|*|*|^M^J")
(puts "|*|*|*|*|*|*|*|*|^M^J")
(puts "|*|*|*|*|*|*|*|*|^M^J")
(puts "|*|*|*|*|*|*|*|*|^M^J")
(puts "|*|*|*|*|*|*|*|*|^M^J")
(puts "|*|*|*|*|*|*|*|*|^M^J")
(puts "`---------------'")
}
)
(defun
print-solution
{
(int j)
(for (j 0)(< j N)(+= j 1) (msg "(" (boardx j) "," (boardy j) ")"))
}
threat (int a b x y)
{
(or
(== a x)(== b y)
(== (- a b)(- x y))
(== (+ a b)(+ x y))
)
}
conflict (int x y)
{
(int n)
(for (n 0)(< n nQueens)(+= n 1)
(if (threat x y (boardx n) (boardy n)) { TRUE (done) }))
FALSE
}
fill-board (int x y)
{
(int i j z)(z x)
(for (i z)(< i N)(+= i 1)
{
(for (j z)(< j N)(+= j 1)
{
(if (conflict i j) ()
{
(put-queen i j nQueens)
(boardx nQueens i)(boardy nQueens j)(+= nQueens 1)
(fill-board i j)
(if (== nQueens N){ TRUE (done) })
(-= nQueens 1)(put-queen i j ".")
})
})
(z 0)
})
FALSE
}
Queens
{
(N (convert-to NUMBER (ask "Number of queens (max 8) = ")))
(if (or (> N 8) (< N 1)) { (msg "Bogus number of queens")(done) })
(nQueens 0)(print-board)
(if (fill-board 0 0) (msg "done")
(msg "No solution"))
}
)
(defun MAIN { (Queens) })